package cn.tedu.antconlonysever.contract.dao.persist.repository.impl;

import cn.tedu.antcolonycommons.pojo.vo.PageData;
import cn.tedu.antcolonycommons.util.PageInfoToPageDataConverter;
import cn.tedu.antconlonysever.contract.dao.persist.mapper.LinkmanMapper;
import cn.tedu.antconlonysever.contract.dao.persist.repository.ILinkmanRepository;
import cn.tedu.antconlonysever.contract.pojo.entity.Linkman;
import cn.tedu.antconlonysever.contract.pojo.vo.LinkmanListVO;
import cn.tedu.antconlonysever.contract.pojo.vo.LinkmanStandardVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;

@Service
@Slf4j
public class LinkmanRepositoryImpl implements ILinkmanRepository {
    @Autowired
    private LinkmanMapper linkmanMapper;
    @Override
    public int insert(Linkman linkman) {
        return linkmanMapper.insert(linkman);
    }

    @Override
    public int insertBatch(List<Linkman> linkmanList) {
        return linkmanMapper.insertBatch(linkmanList);
    }

    @Override
    public int deleteById(Long id) {
        return linkmanMapper.deleteById(id);
    }

    @Override
    public int deleteByIds(Collection<Long> idList) {
        return linkmanMapper.deleteBatchIds(idList);
    }

    @Override
    public int update(Linkman linkman) {
        return linkmanMapper.updateById(linkman);
    }

    @Override
    public int count() {

        return linkmanMapper.selectCount(null);
    }

    @Override
    public int countCusId(Long cusId) {
        QueryWrapper<Linkman> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("cus_id",cusId);
        return linkmanMapper.selectCount(queryWrapper);
    }

    @Override
    public int contactCusIdNotId(Long id, Long cusId) {
        QueryWrapper<Linkman> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("cus_id",cusId)
                    .ne("id",id);
        return linkmanMapper.selectCount(queryWrapper);
    }

    @Override
    public LinkmanStandardVO getStandardById(Long id) {
        return linkmanMapper.getStandardById(id);
    }

    @Override
    public PageData<LinkmanListVO> list(Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum, pageSize);
        List<LinkmanListVO> list = linkmanMapper.list();
        PageInfo<LinkmanListVO> pageInfo=new PageInfo<>(list);
        PageData<LinkmanListVO> pageData = PageInfoToPageDataConverter.convert(pageInfo);
        return pageData;
    }
}
